package com.lxyk.yygh.hosp.controller.api;

import com.lxyk.yygh.common.exception.YyghException;
import com.lxyk.yygh.common.helper.HttpRequestHelper;
import com.lxyk.yygh.common.result.Result;
import com.lxyk.yygh.common.result.ResultCodeEnum;
import com.lxyk.yygh.common.utils.MD5;
import com.lxyk.yygh.hosp.service.DepartmentService;
import com.lxyk.yygh.hosp.service.HospitalService;
import com.lxyk.yygh.hosp.service.HospitalSetService;
import com.lxyk.yygh.hosp.service.ScheduleService;
import com.lxyk.yygh.model.hosp.Department;
import com.lxyk.yygh.model.hosp.Hospital;
import com.lxyk.yygh.model.hosp.Schedule;
import com.lxyk.yygh.vo.hosp.DepartmentQueryVo;
import com.lxyk.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 和hosp-manage的apiController相关，apiController调用此处的controller进行操作
 * 说明：平台对外开发的接口都写在该Controller类
 */
@RestController
@RequestMapping("/api/hosp/")
@Api(tags = "医院管理API接口")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;//签名比对

    @Autowired
    private DepartmentService departmentService;

    //排班
    @Autowired
    private ScheduleService scheduleService;


//    /api/hosp/saveHospital
    @ApiOperation("上传医院接口")
    @PostMapping("/saveHospital")
    public Result  saveHospital(HttpServletRequest request){
        //将请求的params转换
        Map<String, Object> paramsMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取签名,签名已经加密过
        String signKey =(String)paramsMap.get("sign");
        String hoscode =(String)paramsMap.get("hoscode");
        //hospitalservice查询数据库
        String signKeySelect = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKeySelect);

        if(!signKey.equals(signKeyMD5))
        {
            //不相同，抛出异常
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //图片base64编码
        //传输过程中“+”转换为了“ ”，因此要转换回来
        String logoDataString = (String)paramsMap.get("logoData");
        System.out.println(logoDataString);
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            System.out.println(logoData);
            paramsMap.put("logoData", logoData);
        }
        hospitalService.save(paramsMap);
        return Result.ok();
    }

    /**
     * 获取医院信息
     */
    @ApiOperation("获取医院信息")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        String signKey = (String)paramMap.get("sign");
        //对比签名成功之后才进行查询
        String signKeySelect = hospitalSetService.getSignKey(hoscode);
        //数据库查询出来的进行md5进行加密
        String encrypt = MD5.encrypt(signKeySelect);
        if(!signKey.equals(encrypt))
        {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //进行查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 科室对外api  调用/api/hosp/saveDepartment
     */
    @ApiOperation("上传科室信息")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //接口系统 for循环，调用，传递paramsMap
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //都要进行第三方比对
        //获取医院编码
        String hoscode = (String) paramMap.get("hoscode");

        //1 获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名 获取签名key
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * mogodb分页查询
     * 和mybatisplus有点不一样啊··
     * @param request
     * @return
     */
    @ApiOperation("查询department列表")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //获取当前页 page 每页条数 limit
        int current=StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit=StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));

        //TODO :签名校验
        //1 获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名 获取签名key
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //封装vo
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pageModel = departmentService.findPageDepartment(current, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除科室
     * @param request
     * @return
     */
    @ApiOperation("删除科室")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode=(String)paramMap.get("hoscode");
        String depcode=(String)paramMap.get("depcode");
        //TODO 签名校验
        String sign=(String)paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //md5加密后对比
        String signKeyMD5  = MD5.encrypt(signKey);
        if(!sign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //执行删除
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    /**
     * 上传排班
     * @param request
     * @return
     */
    @ApiOperation("上传排班")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");

        //TODO 签名校验
        //1 获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名 获取签名key
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.saveSchedule(paramMap);
        return Result.ok();
    }


    /**
     * 分页查询排班
     * @param request
     * @return
     */
    @ApiOperation(value = "查询排班")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取科室编号
        String depcode = (String) paramMap.get("depcode");

        //当前页：page 和 每页记录数：limit
        int page = StringUtils.isEmpty(paramMap.get("page"))? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //TODO 签名校验
        //1 获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2 根据传递过来医院编码，查询数据库，查询签名 获取签名key
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3 把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);
        return Result.ok(pageModel);

    }

    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取排班编号
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //TODO 签名校验
        //1 获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名 获取签名key
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if(!hospSign.equals(signKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

}
